{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: DateConcept Extraction with References and Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: DateConcept Extraction with References and Justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import DateConcept, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Sample document text containing project timeline information\n",
        "project_text = \"\"\"\n",
        "Project Timeline: Website Redesign\n",
        "\n",
        "The website redesign project officially kicked off on March 1, 2024.\n",
        "The development team has estimated the project will take 4 months to complete.\n",
        "\n",
        "Key milestones:\n",
        "- Design phase: 1 month\n",
        "- Development phase: 2 months  \n",
        "- Testing and deployment: 1 month\n",
        "\n",
        "The marketing team needs the final completion date to plan the launch campaign.\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document from the text\n",
        "doc = Document(raw_text=project_text)\n",
        "\n",
        "# Create a DateConcept to calculate the project completion date\n",
        "completion_date_concept = DateConcept(\n",
        "    name=\"Project completion date\",\n",
        "    description=\"The final completion date for the website redesign project\",\n",
        "    add_justifications=True,  # enable justifications to understand extraction logic\n",
        "    justification_depth=\"balanced\",\n",
        "    justification_max_sents=3,  # allow up to 3 sentences for the calculation justification\n",
        "    add_references=True,  # include references to source text\n",
        "    reference_depth=\"sentences\",  # reference specific sentences rather than paragraphs\n",
        "    singular_occurrence=True,  # extract only one calculated date\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([completion_date_concept])\n",
        "\n",
        "# Configure DocumentLLM\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept\n",
        "completion_date_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the calculated completion date with justification and references\n",
        "print(\"Calculated project completion date:\")\n",
        "extracted_item = completion_date_concept.extracted_items[\n",
        "    0\n",
        "]  # get the single calculated date\n",
        "print(f\"\\nCompletion Date: {extracted_item.value}\")  # expected output: 2024-07-01\n",
        "print(f\"Calculation Justification: {extracted_item.justification}\")\n",
        "print(\"Source references used for calculation:\")\n",
        "for sent in extracted_item.reference_sentences:\n",
        "    print(f\"- {sent.raw_text}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}